#!/usr/bin/env Python
# coding=utf-8
from sqlalchemy import Column
from sqlalchemy.types import CHAR, Integer, String
from sqlalchemy import func, or_, not_

import methods.database.dbORM as dbORM

def init_db():
    dbORM.BaseModel.metadata.create_all(dbORM.engine)

def drop_db():
    dbORM.BaseModel.metadata.drop_all(dbORM.engine)

def getUser(data):
    param=""
    for i in data:
        param=param+","+i+"='"+data[i]+"'"
    query=dbORM.session.query(User).filter(param[1:]).scalar()
    print(query)
    return query

def addUser(data):
    user = User()
    for i in data:
        setattr(user,i,data[i])
    dbORM.session.add(user)
    dbORM.session.commit()
    return dbORM.session.query(func.max(User.id)).one()[0]


class User(dbORM.BaseModel):
    __tablename__ = 'login_user'
    id = Column(Integer, primary_key=True)
    login_name = Column(CHAR(50))
    password = Column(CHAR(50))
    net_name = Column(CHAR(50))

    def __repr__(self):
        return "<User(name='%s',  password='%s',  net_name='%s')>" % (self.login_name, self.password, self.net_name)
